<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/5
 * Time: 11:17
 */

namespace App\Controller;

use View;
use DB;

class AdminArticleController extends AdminBaseController
{

    public function index()
    {
//        $count=DB::table('article')->select('count(*)')->first();
//        print_r($count);
//        die;


        View::set('title', '文章');
        View::set('operation', '列表');
//
//        for ($i = 0; $i < 200; $i++) {
//            DB::table('article')->add([
//                'title' => '测试文章' . $i,
//            ]);
//        }
        $count=DB::table('article')->count();
        if (!$count){
            $artslist=[];
            $pn=false;

        }else{
            $page=1;
            if (isset($_GET['page'])){
                $page=$_GET['page'];

            }
            $page_size=12;
            $artslist = DB::table('article')
                ->skip(($page - 1) * $page_size)
                ->orderBy('id desc')
                //->debug()
                ->get($page_size);
//            1.
//            foreach ($artslist as $art){
//                $user=DB::table('user')
//                    ->select('name')
//                    ->where('id','=',$art['user_id'])
//                    ->first();
//                if($user){
//                    $art['user_name']=$user['name'];
//
//                }else{
//                    $art['user_name']='';
//                }
//
//            }
//            2.
            foreach ($artslist as &$art){
                $user_ids[]=$art['user_id'];
                $cate_ids[]=$art['cate_id'];
            }
            $user=DB::table('user')
                    ->select('name')
                    ->where('id','in',implode(',',$user_ids))
                    ->getAll();
            $user=DB::table('cate')
                ->select('name')
                ->where('id','in',implode(',',$cate_ids))
                ->getAll();



            $pn=$this->getPager($page,$count);
        }
        View::set('pn',$pn);
        View::set('artslist', $artslist);
        View::show('admin/article_list');

    }

    public function addPage()
    {
        View::set('title', '文章');
        View::set('operation', '添加');
        $catelist = DB::table('cate')
            ->orderBy('id desc')
            ->getAll();
        View::set('catelist', $catelist);
        View::show('admin/article_add');

    }

    public function add()
    {

        DB::table('article')->add([
            'title' => $_POST['title'],
            'user_id' => $_SESSION['user_id'],
            'cate_id' => $_POST['cate_id'],
            'content' => $_POST['content'],
        ]);

    }

    public function del($id = null)
    {
        if ($id) {
            DB::table('article')->where('id', '=', $id)->del();
        }
        $this->redirect('/admin/article');
    }


    public function editPage($id=null)
    {
        View::set('title', '文章');
        View::set('operation', '编辑');
        $catelist = DB::table('cate')
            ->orderBy('id desc')
            ->getAll();

        $art=DB::table('article')->where('id','=',$id)->first();

        View::set('art',$art);

        View::set('catelist', $catelist);
        View::show('admin/article_edit');

    }

    public function update($id=null)
    {

        DB::table('article')
            ->where('id','=',$id)
            ->update([
            'title' => $_POST['title'],
            'user_id' => $_SESSION['user_id'],
            'cate_id' => $_POST['cate_id'],
            'content' => $_POST['content'],
        ]);


        $this->msg('文字更新成功','/admin/article','文章列表');

    }
}